package com.example.careycai.playactivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity {

    private EditText et1,et2,et3;
    private Button btn1;
    private CheckBox ct;
    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_register);
        dbHelper=new MyDatabaseHelper(this,"Play.db",null,1);
        Goto();
    }

     private void Goto() {
        btn1=(Button)findViewById(R.id.button);
        et1=(EditText)findViewById(R.id.editText);
        et2=(EditText)findViewById(R.id.editText2);
        et3=(EditText)findViewById(R.id.editText3);
        ct=(CheckBox)findViewById(R.id.checkBox);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et1.getText().toString().trim().length()!=0&&et2.getText().toString().trim().length()!=0&&
                        et3.getText().toString().trim().length()!=0){
                    if(et2.getText().toString().equals(et3.getText().toString())){
                        if(ct.isChecked()){
                            SQLiteDatabase db=dbHelper.getWritableDatabase();
                            ContentValues values=new ContentValues();
                            values.put("name",et1.getText().toString());
                            values.put("password", et2.getText().toString());
                            db.insert("userinfo", null, values);
                            values.clear();

                            Toast.makeText(Register.this,"恭喜你注册成功",Toast.LENGTH_LONG).show();
                            Intent intent=new Intent(Register.this,Login.class);
                            startActivity(intent);
                            finish();
                        }
                        else{
                            AlertDialog.Builder dialog=new AlertDialog.Builder(Register.this);
                            dialog.setTitle("警告");
                            dialog.setIcon(R.mipmap.ic_launcher);
                            dialog.setMessage("请阅读并同意隐藏政策，服务条款");
                            dialog.create();
                            dialog.show();
                        }
                    }else{
                        AlertDialog.Builder dialog=new AlertDialog.Builder(Register.this);
                        dialog.setTitle("警告");
                        dialog.setIcon(R.mipmap.ic_launcher);
                        dialog.setMessage("两次密码不一致");
                        dialog.create();
                        dialog.show();
                    }
                }else{
                    AlertDialog.Builder dialog=new AlertDialog.Builder(Register.this);
                    dialog.setTitle("警告");
                    dialog.setIcon(R.mipmap.ic_launcher);
                    dialog.setMessage("输入用户名或密码不能为空");
                    dialog.create();
                    dialog.show();
                }
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_register, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
